<?php

include_once(dirname(__FILE__). '/AController.php');

class OrderController extends AController {
	
	/**
	 * 来自所有推广账号的订单列表
	 */
	public function actionList() {
		$block_title = '订单列表';
		$arr = array(
			'pagination' => array('pageSize' => 20),
			'criteria' => array('order' => 'order_history_id DESC', 'condition' => '1')
		);
		if (key_exists('verify_status', $_GET)) {
			$arr['criteria']["condition"] .= " AND verify_status='{$_GET['verify_status']}'";
			$str = array('0' => ' - 待审核', '1' => ' - 有效', '-1' => ' - 无效');
			$block_title .= $str[$_GET['verify_status']];
		}
		if (!empty($_GET['owner_id'])) {
			$arr['criteria']['condition'] .= " AND owner_id='{$_GET['owner_id']}'";
		}
		$dataProvider = new CActiveDataProvider('OrderHistory', $arr);
		$this->render('list', array('block_title' => '订单列表', 'dataProvider' => $dataProvider));
	}
	
	/**
	 * 来自所有推广账号的首次订单列表
	 */
	public function actionListFirst() {
		$block_title = '首次订单列表';
		$arr = array('pagination' => array('pageSize' => 20));
		$count_sql = "SELECT count(distinct(f.order_id)) FROM first_order f, order_history o WHERE f.order_id=o.order_id";
		$sql = "SELECT * FROM first_order f, order_history o WHERE f.order_id=o.order_id";
		if (key_exists('verify_status', $_GET)) {
			if ($_GET['verify_status'] == 0) {
				$n = Yii::app()->params['daysAfterFirstOrder'];
				$date = date('Y-m-d', strtotime("-$n days"));
				$sql .= " AND adddate<='$date'";
				$count_sql .= " AND adddate<='$date'";
			}
			$sql .= " AND verify_status=".intval($_GET['verify_status']);
			$count_sql .= " AND verify_status=".intval($_GET['verify_status']); 
			$str = array('0' => ' - 待审核', '1' => ' - 有效', '-1' => ' - 无效');
			$block_title .= $str[$_GET['verify_status']];
		}
		if (!empty($_GET['owner_id'])) {
			$sql .= " AND owner_id='{$_GET['owner_id']}'";
			$count_sql .= " AND owner_id='{$_GET['owner_id']}'";
		}
		$sql .= " Order By order_history_id DESC";
		$arr['totalItemCount'] = Yii::app()->db->createCommand($count_sql)->queryScalar();
		$dataProvider = new QpidSqlDataProvider($sql, $arr);
		$this->render('list', array('block_title' => $block_title, 'dataProvider' => $dataProvider));
	}
	
	/**
	 * 来自某个推广账号的订单列表
	 */
	public function actionOwnerList() {
		$this->layout = '/layouts/popMain';
		$this->actionList();
	}
	
	/**
	 * 来自所有推广账号的首次订单列表
	 */
	public function actionOwnerListFirst() {
		$this->layout = '/layouts/popMain';
		$this->actionListFirst();
	}
	
	/**
	 * 更新订单的有效状态
	 */
	public function actionUpdate() {
		if (key_exists('verify_status', $_GET) && !empty($_POST['orders'])) {
			$trans = Yii::app()->db->beginTransaction();
			try {
				foreach ($_POST['orders'] as $k => $id) {
					$order = OrderHistory::model()->findByPk($id);
					
					$num = 0;
					$amount = 0;
					if ($_GET['verify_status'] == 1 && $order->verify_status != 1) {
						$num = 1;
						$amount = $order->amount;
						$order->verifydate = date('Y-m-d');
					} else if ($_GET['verify_status'] != 1 && $order->verify_status == 1) {
						$num = 0;
						$amount = - $order->amount;
						$order->verifydate = '0000-00-00';
					}
					
					//是否为首次订单?
					$first_order = FirstOrder::model()->find('order_id=:order_id', array(':order_id' => $order->order_id));
					
					$diary_log = DiaryLog::load($order->owner_id, $order->website_id, $order->adddate);
					$diary_log->valid_ordernum += $num;	
					$diary_log->valid_orderamount += $amount;
					if ($first_order) {
						$diary_log->valid_firstordernum += $num;
						$diary_log->valid_firstorderamount += $amount;
					}
					$diary_log->save();
					
					$diary_log2 = DiaryLog::load($order->owner_id, $order->website_id, date('Y-m-d'));
					$diary_log2->check_ordernum += $num;
					$diary_log2->check_orderamount += $amount;
					if ($first_order) {
						$diary_log2->check_firstordernum += $num;
						$diary_log2->check_firstorderamount += $amount;
					}
					$diary_log2->save();
					
					/*
					 这里说明一下 check_ordernum 和  valid_ordernum 的区别：
					 check_ordernum 是指在某天被审核的订单数
					 valid_ordernum 是指在 某天的有效订单数
					 */
					
					// TODO 广告的有效订单统计
					
					$order->verify_status = $_GET['verify_status'];
					$order->save();
				}
				$trans->commit();
			} catch (Exception $e) {
				$trans->rollback();
			}
		}
		$this->redirect(array('listFirst'));
	}
}

